import { Body, Controller, Post, Get, Query, Put, Param, Delete } from '@nestjs/common';
import { PointsGoodsService } from './points-goods.service';
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiOperation, ApiTags, ApiResponse } from '@nestjs/swagger';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { CreatePointsGoodsDto, FindPointsGoodsListDto, UpdatePointsGoodsDto } from './points-goods.dto';
import { AllowAnon } from '~/modules/auth/decorators/allow-anon.decorator';
import { StandardPagination } from '~/helper/paginate';
import { PointsGoodsEntity } from './points-goods.entity';
import { AuthTenant } from '~/modules/tenant/tenant.decorator';
import { ApiTenantHeader } from '~/common/decorators/api-header-tenant.decorator';

@ApiTags(ModuleSwaggerTags.POINTS)
@ApiBearerAuth()
@Controller()
export class PointsGoodsController {
  constructor(private readonly pointsGoodsService: PointsGoodsService) {}

  @Post('add')
  @AllowAnon()
  @ApiOperation({ summary: '新增商品' })
  @ApiBody({ type: CreatePointsGoodsDto })
  async createGoods(
    @Body() createPointsGoodsDto: CreatePointsGoodsDto,
    @AuthTenant() tenant: IAuthTenant,
  ) {
    return await this.pointsGoodsService.create(createPointsGoodsDto, tenant?.id ?? 1);
  }

  @Put('update/:id')
  @AllowAnon()
  @ApiOperation({ summary: '更新商品' })
  @ApiBody({ type: UpdatePointsGoodsDto })
  async updateGoods(
    @Param('id') id: number,
    @Body() updatePointsGoodsDto: UpdatePointsGoodsDto,
    @AuthTenant() tenant: IAuthTenant,
  ) {
    return await this.pointsGoodsService.update(id, updatePointsGoodsDto, tenant?.id ?? 1);
  }

  @Get('page')
  @ApiOperation({ summary: '分页查询积分商品列表' })
  @ApiResponse({
    status: 200,
    description: '查询成功',
    type: StandardPagination<PointsGoodsEntity>,
  })
  @ApiTenantHeader()
  async findPage(
    @Query() dto: FindPointsGoodsListDto,
    @AuthTenant() tenant: IAuthTenant,
  ): Promise<StandardPagination<PointsGoodsEntity>> {
    return await this.pointsGoodsService.findPage(dto, tenant.id);
  }

  @Delete('delete/:id')
  @AllowAnon()
  @ApiOperation({ summary: '删除商品' })
  @ApiResponse({
    status: 200,
    description: '删除成功',
    schema: {
      type: 'object',
      properties: {
        message: {
          type: 'string',
          example: '商品删除成功'
        }
      }
    }
  })
  async deleteGoods(
    @Param('id') id: number,
    @AuthTenant() tenant: IAuthTenant,
  ) {
    return await this.pointsGoodsService.delete(id, tenant?.id ?? 1);
  }
}
